package com.ruoyi.project.erp.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.erp.domain.Project;
import com.ruoyi.project.erp.mapper.ProjectMapper;
import com.ruoyi.project.erp.mapper.ProjectUserMapper;
import com.ruoyi.project.erp.mapper.TaskMemberMapper;
import com.ruoyi.project.erp.mapper.WorklogMapper;
import com.ruoyi.project.erp.service.IProjectService;
import com.ruoyi.project.erp.service.ITaskService;
import com.ruoyi.project.erp.service.IWorklogService;
import com.ruoyi.project.erp.vo.SysUserVo;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/erp/user")
public class ErpUserController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ProjectUserMapper projectUserMapper;

    @Autowired
    private TaskMemberMapper taskMemberMapper;

    @Autowired
    private WorklogMapper worklogMapper;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('erp:project:list')")
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        AjaxResult ajax = AjaxResult.success();
        // 获取当前登录用户信息
        LoginUser user = SecurityUtils.getLoginUser();
        SysUser sysUser = userService.selectUserById(user.getUserId());
        ajax.put("userInfo", sysUser);
        // 查询当前用户参与的项目
        int projectNum = projectUserMapper.selectProjectByDdUserIdCount(user.getUser().getDdUserId());
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(sysUser);
        ajax.put("projectNum", projectNum);
        // 查询当前用户提报的工时
        BigDecimal hours = worklogMapper.getWorkLogHoursByUserId(user.getUser().getDdUserId());
        ajax.put("permissions", permissions);
        ajax.put("workLogNum", hours);
        // 查询当前用户参与的任务
        int taskNum = taskMemberMapper.selectTaskMemberByUserIdCount(user.getUser().getDdUserId());
        ajax.put("taskNum", taskNum);
        return ajax;
    }
}
